package Servlets.yz;

import Bean.User;
import Bean.supplier;
import Util.GokJDBCUtil2;
import com.mysql.cj.util.StringUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
/*
* 用于实现用户展示功能
* */
@WebServlet(name = "UserShowServlet",urlPatterns = "/UserShowServlet")
public class UserShowServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String method = req.getParameter("method");//获取查询方法
        String username = req.getParameter("username");//获取用户的名字
        String phone = req.getParameter("phone");//获取用户的电话
        List<User> users = null;//存储查询到的元素

        /*分页*/
        int page=1;
        int pageSize=4;
        //计算总记录数量
        String countSql = "select count(*) from user where isdeleted=0";
        int totalcount = GokJDBCUtil2.queryForCount(countSql);
        int totalpages = ((totalcount + pageSize - 1) / pageSize);

        String currentpage = req.getParameter("page");
        //在前端获取到的page为空时默认为1
        if(req.getParameter("page")==null){
            page=1;
        }
        //page不为空的情况，将获取到的page数据赋值到后端的page
        else {
            page=Integer.parseInt(currentpage);
            if(page > totalpages){
                page=totalpages;
            }
        }
        //设置开始索引
        int startIndex = (page-1)*pageSize;
        /*
         * 用于实现1-2种的信息模糊查询
         * */
        if("select".equals(method)){
            //检测从最上面获取到的username和phone是否都不为空
            if(!StringUtils.isNullOrEmpty(username) && !StringUtils.isNullOrEmpty(phone)){
                String sql = "select * from user where username like ? and userphone like ? limit ?,?";
                users = GokJDBCUtil2.queryAll(sql, User.class, "%" + username + "%", "%" + phone + "%",startIndex,pageSize);
                req.setAttribute("users",users);
                req.setAttribute("username",username);
                req.setAttribute("phone",phone);
            }
            //检测从最上面获取到的username是否不为空
            else if(!StringUtils.isNullOrEmpty(username)){
                String sql = "select * from user where username like ? limit ?,?";
                users = GokJDBCUtil2.queryAll(sql, User.class, "%" + username + "%",startIndex,pageSize);
                req.setAttribute("users",users);
                req.setAttribute("username",username);
            }
            //检测从最上面获取到的phone是否不为空
            else {
                String sql = "select * from user where userphone like ? limit ?,?";
                users = GokJDBCUtil2.queryAll(sql, User.class, "%" + phone + "%",startIndex,pageSize);
                req.setAttribute("users", users);
                req.setAttribute("phone",phone);
            }
        }
        /*
         * 不进行查询功能的情况直接展示全部信息（分页）
         * */
        else {
            String sql = "select * from user  where isdeleted=0 limit ?,?";
            users = GokJDBCUtil2.queryAll(sql, User.class,startIndex,pageSize);//获取供应商全部信息
            req.setAttribute("users",users);//该数据用于发送用户的全部信息
        }
        req.setAttribute("totalcount",totalcount);//传输总记录数
        req.setAttribute("totalpages",totalpages);//传输总页数
        req.setAttribute("page",page);//传输当前页数
        req.getRequestDispatcher("pages/userManager.jsp").forward(req,resp);//转到供应商展示界面
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
